package com.tai.session;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * The type Spring session controller.
 *
 * @Description :  TODO
 * @Author :  ldt
 * @CreateTime :  2023/5/15
 */
@RestController
@RequestMapping("/session")
public class SpringSessionController {

    /**
     * Gets sesseion.
     *
     * @param request the request
     * @param name    the name
     * @return the sesseion
     */
    @RequestMapping("/get/{name}")
    public String getSesseion(HttpServletRequest request, @PathVariable("name") String name){
        HttpSession session = request.getSession();
        String value = (String)session.getAttribute(name);
        return "sessionId:"+session.getId()+" value:"+value;
    }

    /**
     * Add session string.
     *
     * @param request the request
     * @param name    the name
     * @param value   the value
     * @return the string
     */
    @RequestMapping("/add/{name}/{value}")
    public String addSession(HttpServletRequest request,@PathVariable("name") String name,@PathVariable("value") String value){
        HttpSession session = request.getSession();
        session.setAttribute(name,value);
        return "sessionId:"+session.getId()+" name:"+name;
    }
}
